package com.github.nuggets.repository;


import com.github.nuggets.entity.AuthenticAccount;
import com.github.nuggets.entity.Account;
import com.github.nuggets.repository.client.AccountClient;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.core.userdetails.UsernameNotFoundException;
import org.springframework.stereotype.Component;

import java.util.Optional;

/**
 * @description:
 * @author: wangsj
 * @date: 2022/3/15 7:27 下午
 */
@Component
public class AuthenticUserRepository {

    @Autowired
    AccountClient userClient;

    public Optional<AuthenticAccount> findByUsername(String userName) {
        Optional<Account> user = Optional.ofNullable(userClient.findByUserName(userName));
        return Optional.ofNullable(new AuthenticAccount(user
                .orElseThrow(() -> new UsernameNotFoundException("未找到该用户:" + userName))
        ));
    }

}
